import Ember from 'ember';

export default Ember.Component.extend({
  store: Ember.inject.service("store"),
  statusService: Ember.inject.service("current-status"),
  global_curStatus: Ember.inject.service("current-status"),
  service_notification:Ember.inject.service("notification"),
  hasDataFlag:0,
  videoBoxBig:true,
  maxLoadingFlag:3,
  // showBoxFirstFlag:false,//是否第一次显示此组件
  isShowVideoDetail:false,//PC视频直播全局的弹框 显示标志位
  resetVideoSize(myPlayer){
      // var videoJsBoxWidth = $(window).width();
      // var ratio = 1440/800;
      // var videoJsBoxHeight = videoJsBoxWidth/ratio/2;
      // console.log("videoJsBoxHeight in:",videoJsBoxHeight);
      // if(myPlayer){
      //   myPlayer.height(videoJsBoxHeight);
      // }else{
      //   $(".video-js-pc").height(videoJsBoxHeight);
      //   // $(".video-js-pc .vjs-tech").height(videoJsBoxHeight);
      // }
  },
  resetVideoSizeToAuto(videoId){
      // if(videoId){
      //   $("#"+videoId).css("height", "auto");
      // }else{
      //   $(".video-js-pc").css("height", "auto");
      // }
  },
  resetPlay(myPlayer){
    myPlayer.play();
  },
  init: function(){
    let _self = this;
    this._super(...arguments);
    let socketObjList = new Ember.A();
    this.set("socketObjList",socketObjList);
    let maxDeviceNumber = _self.get("global_curStatus.maxDeviceNumber");
    let maxDeviceList = new Ember.A();
    let num = 0;
    while (num < maxDeviceNumber) {
      let item = Ember.Object.create({
        number:num,
        loadingNumber:0,
        videoId:"video-" + num,
        device:null,
      });
      maxDeviceList.pushObject(item);
      num++;
    }
    this.set("maxDeviceList",maxDeviceList);
  },

  didInsertElement:function(){
    let _self = this;
    _self.resetVideoSize();
    $(window).on("resize", function(){
      _self.resetVideoSize();
    });
    _self.get("service_notification").registNoticePage(function(msgObj){
      //socket筛选
      if(msgObj.code == '1104'){
        let data = JSON.parse(msgObj.content);
        let dataItem = Ember.Object.create({
          showUrl:data.showUrl,
          cameraId:data.cameraId,
          success:data.success
        });
        _self.hasDataFun(dataItem,false);
        // _self.incrementProperty("hasDataFlag");
      }
    });
    let maxLoadingFlag = _self.get("maxLoadingFlag");
    let maxDeviceList = _self.get("maxDeviceList");
    maxDeviceList.forEach(function(item){
      let videoId = item.get("videoId");
      let myPlayer = videojs(videoId);
      item.set("videoJsObj",myPlayer);
      let number = item.get("number");
      let loadingNumber = item.get("loadingNumber");
      console.log("number in loadingNumber:",loadingNumber);
      console.log("number in maxDeviceList:",number);
      myPlayer.on('error', function() {
        console.log('The following error occurred:', this.error());
        let myPlayer = this;
        myPlayer.reset();
        let maxDeviceList = _self.get("maxDeviceList");
        let socketObjList = _self.get("socketObjList");
        console.log("maxDeviceList in error:",maxDeviceList);
        console.log("socketObjList in error:",socketObjList);
        let maxDevice = maxDeviceList.findBy("number",number);
        console.log("maxDevice in error:",maxDevice);
        let loadingNumber = maxDevice.get("loadingNumber");
        maxDevice.get("device").set("hasConnectFlag",false);
        if(loadingNumber < maxLoadingFlag){
          maxDevice.set("loadingNumber",loadingNumber + 1);
          let deviceId = maxDevice.get("device.deviceId");
          console.log("deviceId in error:",deviceId);
          let socketObj = socketObjList.findBy("cameraId",Number(deviceId));
          console.log("socketObj in error:",socketObj);
          Ember.run.later(function(){
            myPlayer.src({
              src: socketObj.get("showUrl"),
              type: 'application/x-mpegURL',
            });
            _self.resetPlay(myPlayer);
            maxDevice.get("device").set("hasConnectFlag",true);
          },2000);
        }else{
          maxDevice.get("device").set("hasNoConnectFlag",true);
        }
      });
    });
  },
  // socket传数据，开始播放视频
  hasDataFun(msgObj,flag){
    let _self = this;
    console.log("flag in fun:",flag);
    console.log("msgObj in hasDataFun:",msgObj);
    let cameraId = msgObj.get("cameraId");
    let successFlag = msgObj.get("success");
    console.log("msgObj.cameraId:",msgObj.get("cameraId"));
    console.log("deviceObj:",_self.get("deviceObj"));
    let myPlayer;
    // 做重复发送和超出外验证
    let maxDeviceList = _self.get("maxDeviceList");
    // let maxDevice = maxDeviceList.findBy("deviceId",Number(cameraId));
    console.log("maxDeviceList in msgObj:",maxDeviceList);
    maxDeviceList.forEach(function(item){
      let deviceId = item.get("device.deviceId");
      if(deviceId == cameraId){
        myPlayer = item.get("videoJsObj");
      }
    });
    console.log("myPlayer in msgObj:",myPlayer);
    myPlayer.ready(function(){
      let myPlayer = this;
      console.log("myPlayer in on:",myPlayer);
      Ember.run.later(function(){
        if(successFlag == 1){
          myPlayer.src({
            src: msgObj.get("showUrl"),
            type: 'application/x-mpegURL',
          });
          // _self.resetVideoSizeToAuto(videoId);
          _self.resetPlay(myPlayer);
        }
        console.log("deviceObj in 11:",_self.get("deviceObj"));
        // let curDevice = _self.get("deviceObj").findBy("deviceId",Number(cameraId));
        let curDevice;
        _self.get("deviceObj").forEach(function(item){
          if(item.get("deviceId") == cameraId){
            console.log("iten in resetPlay:",item);
            curDevice = item;
          }
        });
        console.log("deviceObj in curDevice:",curDevice);
        if(successFlag == 1){
          curDevice.set("hasConnectFlag",true);
        }else if (successFlag == -1) {
          curDevice.set("hasConnectFlag",true);
          curDevice.set("hasLimitFlag",true);
        }else if (successFlag === 0) {
          curDevice.set("hasConnectFlag",true);
          curDevice.set("hasFailFlag",true);
        }
        // try{
        //   _self.resetPlay(myPlayer,cameraId);
        // }catch(e){
        //   console.log("play fail",e);
        //   myPlayer.pause();
        //   Ember.run.later(function(){
        //     _self.resetPlay(myPlayer,cameraId);
        //   },2000);
        // }
      },5000);
    });
    if(!flag){
      let socketObjList = _self.get("socketObjList");
      socketObjList.pushObject(msgObj);
    }

  },
  // socket传数据，开始播放视频
  closeVideoFun(deviceIdStr){
    let _self = this;
    _self.get('store').query('device', {
      filter: {
        queryType: deviceIdStr
      }
    }).then(function(deviceList) {
    });

  },

  deviceObjObs:function(){
    let _self = this;
    console.log("deviceObjFlag before:",this.get("deviceObjFlag"));
    if(!this.get("deviceObjFlag")){
      return ;
    }
    let maxDeviceList = _self.get("maxDeviceList");
    maxDeviceList.forEach(function(item){
      item.set("device",null);
      item.set("loadingNumber",0);
    });
    let deviceObj = this.get("deviceObj");
    let k = 0;
    deviceObj.forEach(function(device){
      maxDeviceList.objectAt(k).set("device",device);
      k++;
    });
    console.log("maxDeviceList after:",maxDeviceList);
    console.log("deviceObj:",deviceObj);
    let length = deviceObj.get("length");
    console.log("length:",length);
    _self.send("switchVideoBoxToBig");
    // Ember.run.later(function(){
    //   _self.resetVideoSize();
    // },500);
    let deviceIdStr = "startLive@did";
    let socketObjList = _self.get("socketObjList");
    console.log("socketObjList in query:",socketObjList);
    if(!socketObjList.get("length")){
      deviceObj.forEach(function(device){
        // device.set("hasConnectFlag",false);
        deviceIdStr += device.get("deviceId") + ",";
      });
      console.log("deviceIdStr11:",deviceIdStr);
    }else{
      deviceObj.forEach(function(device){
        console.log("socketObjList in query11:",socketObjList);
        var socketObj = socketObjList.findBy("cameraId",Number(device.get("deviceId")));
        // var socketObj = socketObjList.filter(function(socketObj) {
        //   return socketObj.get("cameraId") === device.get("deviceId");
        // });
        console.log("socketObj in foreach:",socketObj);
        // let socketObj = socketObjList.findBy("cameraId",device.get("deviceId"));
        // device.set("hasConnectFlag",false);
        if(!socketObj){
          deviceIdStr += device.get("deviceId") + ",";
        }else{
          _self.hasDataFun(socketObj,true);
        }
      });
      console.log("deviceIdStr22:",deviceIdStr);
      socketObjList.forEach(function(socketObj){
        let device = deviceObj.findBy("deviceId",Number(socketObj.get("cameraId")));
        if(!device){
          socketObjList.removeObject(socketObj);
        }
      });

    }
    let substrDeviceIdStr = deviceIdStr.substr(13,deviceIdStr.length-1);
    console.log("substrDeviceIdStr:",substrDeviceIdStr);
    if(substrDeviceIdStr){
      deviceIdStr = deviceIdStr.substr(0,deviceIdStr.length-1);
      _self.get('store').query('device', {
        filter: {
          queryType: deviceIdStr
        }
      }).then(function(deviceList) {
        console.log("error");
      });
    }
  }.observes('deviceObjFlag').on("init"),
  actions:{
    closeVideoBox(maxDevice){
      let _self = this;
      console.log("closeVideoBox in com");
      let deviceId = maxDevice.get("device.deviceId");
      let deviceIdStr = "endLive@did" + deviceId;
      this.closeVideoFun(deviceIdStr);
      let deviceObj = this.get("deviceObj");
      maxDevice.get("device").set("hasSelected",false);
      deviceObj.removeObject(maxDevice.get("device"));
      maxDevice.set("device",null);
      let myPlayer = maxDevice.get("videoJsObj");
      myPlayer.reset();
      // if(deviceObj.get("length") == 1){
      //   console.log("run in 1");
      //   Ember.run.later(function(){
      //     _self.resetVideoSize();
      //   },300);
      // }
      let socketObjList = _self.get("socketObjList");
      socketObjList.forEach(function(socketObj){
        if(socketObj.get("cameraId") == deviceId){
          socketObjList.removeObject(socketObj);
        }
      });
      if(!deviceObj.get("length")){
        this.set("isShowVideoDetail",false);
      }
    },
    closeAllVideoBox(){
      let _self = this;
      console.log("closeAllVideoBox in com");
      let deviceIdStr = "endLive@did";
      let deviceObj = this.get("deviceObj");
      let socketObjList = _self.get("socketObjList");
      socketObjList = new Ember.A();
      this.set("socketObjList",socketObjList);
      console.log("deviceObj in close:",deviceObj);
      deviceObj.forEach(function(device){
        deviceIdStr += device.get("deviceId") + ",";
        device.set("hasSelected",false);
        // deviceObj.removeObject(device);
      });
      deviceIdStr = deviceIdStr.substr(0,deviceIdStr.length-1);
      this.closeVideoFun(deviceIdStr);
      // this.set("deviceObj",null);
      let maxDeviceList = _self.get("maxDeviceList");
      maxDeviceList.forEach(function(item){
        item.set("device",null);
        let myPlayer = item.get("videoJsObj");
        myPlayer.reset();
      });
      this.set("isShowVideoDetail",false);
    },
    switchVideoBoxToSmall(){
      console.log("switchVideoBoxToSmall in com");
      this.set("videoBoxBig",false);
      console.log("videoBoxBig:",this.get("videoBoxBig"));
    },
    switchVideoBoxToBig(){
      let _self = this;
      console.log("switchVideoBoxToBig in com");
      this.set("videoBoxBig",true);
      // Ember.run.later(function(){
      //   _self.resetVideoSize();
      // },300);
    },

  }
});
